<?php

namespace app\admin\controller;

use app\common\controller\Backend;

/**
 * 园区
 *
 * Class Garden
 * @package app\admin\controller
 */
class Garden extends Backend
{
    /**
     * @var \app\common\model\Garden
     */
    protected $model = null;
    protected $searchFields = 'number,name';
    protected $noNeedRight = ['searchList', 'cxSelect'];
    protected $schoolLimit = true;
    protected $modelValidate = true;
    protected $modelSceneValidate = true;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = model('Garden');
    }

    /**
     * 查看
     */
    public function index()
    {
        //设置过滤方法
        $this->request->filter(['strip_tags']);
        if ($this->request->isAjax()) {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
            $total = $this->model
                ->with('school')
                ->where($where)
                ->order($sort, $order)
                ->count();

            $list = $this->model
                ->with('school')
                ->where($where)
                ->order($sort, $order)
                ->limit($offset, $limit)
                ->select();

            $list = collection($list)->toArray();
            $result = array("total" => $total, "rows" => $list);

            return json($result);
        }
        return $this->view->fetch();
    }

    /**
     * 联动搜索
     */
    public function cxSelect()
    {
        $school_id = $this->request->request('school_id');
        $result = $this->model->where('school_id', $school_id)->select();

        $searchList = [];
        foreach ($result as $key => $value) {
            $searchList[] = ['value' => $value['id'], 'name' => $value['name']];
        }

        $this->success('', null, $searchList);
    }
}
